<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <!-- vue3删除了过滤器，多数情况下可以使用计算属性来代替 -->
      <!-- 如果出现在了v-for里面，计算不好用的时候，可以使用methods -->
      <h2>过滤器</h2>
      <p>性别: {{ sex2 }}</p>
      <p>当前时间: {{ formatTime }}</p>

      <ul>
        <li v-for="item in arr" :key="item.name">
          {{ item.name }}的性别是{{ fn(item.sex) }}
        </li>
      </ul>
    </div>

    <script src="./lib/vue3.js"></script>
    <script>
      const { createApp } = Vue;

      createApp({
        data() {
          return {
            sex: 1,
            time: 1669258007318,
            arr: [
              {
                name: "lilei",
                sex: 1,
              },
              {
                name: "hanmeimei",
                sex: 2,
              },
            ],
          };
        },
        computed: {
          sex2() {
            return this.sex === 1 ? "男" : "女";
          },
          formatTime() {
            const date = new Date(this.time);
            const Y = date.getFullYear();
            const M = date.getMonth() + 1;
            const D = date.getDate();
            const hour = date.getHours();
            const min = date.getMinutes();
            const sec = date.getSeconds();
            return (
              Y +
              "-" +
              (M < 10 ? "0" + M : M) +
              "-" +
              (D < 10 ? "0" + D : D) +
              " " +
              (hour < 10 ? "0" + hour : hour) +
              ":" +
              (min < 10 ? "0" + min : min) +
              ":" +
              (sec < 10 ? "0" + sec : sec)
            );
          },
        },
        methods: {
          fn(v) {
            return v === 1 ? "男" : "女";
          },
        },
      }).mount("#app");
    </script>
  </body>
</html>
